package com.wanggsx.library.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.wanggsx.library.R;
import com.wanggsx.library.base.app.init.AppConstant;

import java.util.List;

import static android.app.Activity.RESULT_OK;

public class UtilsImagePicker {

    private static PictureSelectionModel translateCameraOrGallery(Activity activity, boolean isUsingCamera) {
        PictureSelectionModel model = null;
        if (isUsingCamera) {
            if (UtilsPermission.isGrantCamera(activity) && UtilsPermission.isGrantWriteExternalStorage(activity)) {
                model = PictureSelector.create(activity).openCamera(PictureMimeType.ofImage());
            }
        } else {
            if (UtilsPermission.isGrantWriteExternalStorage(activity)) {
                model = PictureSelector.create(activity).openGallery(PictureMimeType.ofImage());
            }
        }
        return model;
    }

    public static void selectOneImage(Activity activity, boolean isUsingCamera) {
        PictureSelectionModel model = translateCameraOrGallery(activity, isUsingCamera);
        if (model != null)
            model.selectionMode(PictureConfig.SINGLE)
                    .isCamera(true)
                    .setOutputCameraPath(AppConstant.folderName)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public static void selectManyImage(Activity activity, boolean isUsingCamera, int maxSize) {
        PictureSelectionModel model = translateCameraOrGallery(activity, isUsingCamera);
        if (model != null)
            model.selectionMode(PictureConfig.MULTIPLE)
                    .isCamera(true)
                    .maxSelectNum(maxSize)
                    .setOutputCameraPath(AppConstant.folderName)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public static void selectOneImageOrVideo(Activity activity, boolean isUsingCamera) {
        PictureSelectionModel model = translateCameraOrGallery(activity, isUsingCamera);
        if (model != null)
            model.selectionMode(PictureConfig.SINGLE)
                    .isCamera(true)
                    .setOutputCameraPath(AppConstant.folderName)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public static void selectManyImageOrVideo(Activity activity, boolean isUsingCamera, int maxSize) {
        PictureSelectionModel model = translateCameraOrGallery(activity, isUsingCamera);
        if (model != null)
            model.selectionMode(PictureConfig.MULTIPLE)
                    .isCamera(true)
                    .maxSelectNum(maxSize)
                    .setOutputCameraPath(AppConstant.folderName)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    public static List<LocalMedia> getRealpathFromMedia(int requestCode, int resultCode, Intent data) {
        if (requestCode == PictureConfig.CHOOSE_REQUEST && resultCode == RESULT_OK) {
            // 图片、视频、音频选择结果回调
            return PictureSelector.obtainMultipleResult(data);
        }
        return null;
    }

    /**
     * 对话框
     */
    private static Dialog mDialog;

    /**
     * 关闭对话框
     */
    public static void closeDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
    }

    /**
     * 弹框让用户选择拍照还是选择照片
     */
    public static void chooseChooseSelectionMode(Activity activity, boolean isJustImage, final int count) {
        closeDialog();
        mDialog = new Dialog(activity, R.style.ActionSheetDialogStyle);
        View inflate = LayoutInflater.from(activity).inflate(R.layout.dialog_pic_gain, null);
        mDialog.setContentView(inflate);
        Window dialogwindow = mDialog.getWindow();
        dialogwindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogwindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogwindow.setAttributes(lp);
        mDialog.show();
        TextView tv_close, pat_pic, photo_select;
        tv_close = inflate.findViewById(R.id.tv_close);
        photo_select = inflate.findViewById(R.id.photo_select);
        pat_pic = inflate.findViewById(R.id.pat_pic);
        tv_close.setOnClickListener(view -> mDialog.dismiss());
        pat_pic.setOnClickListener(view -> {//照片
            mDialog.dismiss();

            if (count > 1) {
                if (isJustImage) {
                    selectManyImage(activity, true, count);
                } else {
                    selectManyImageOrVideo(activity, true, count);
                }
            } else if (count == 1) {
                if (isJustImage) {
                    selectOneImage(activity, true);
                } else {
                    selectOneImageOrVideo(activity, true);
                }
            }
        });
        photo_select.setOnClickListener(view -> {//相册
            mDialog.dismiss();
            if (count > 1) {
                if (isJustImage) {
                    selectManyImage(activity, false, count);
                } else {
                    selectManyImageOrVideo(activity, false, count);
                }
            } else if (count == 1) {
                if (isJustImage) {
                    selectOneImage(activity, false);
                } else {
                    selectOneImageOrVideo(activity, false);
                }
            }
        });
    }

}

/**
 * // 进入相册 以下是例子：用不到的api可以不写
 * PictureSelector.create(MainActivity.this)
 * .openGallery()//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
 * .theme()//主题样式(不设置为默认样式) 也可参考demo values/styles下 例如：R.style.picture.white.style
 * .maxSelectNum()// 最大图片选择数量 int
 * .minSelectNum()// 最小选择数量 int
 * .imageSpanCount(4)// 每行显示个数 int
 * .selectionMode()// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
 * .previewImage()// 是否可预览图片 true or false
 * .previewVideo()// 是否可预览视频 true or false
 * .enablePreviewAudio() // 是否可播放音频 true or false
 * .isCamera()// 是否显示拍照按钮 true or false
 * .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
 * .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
 * .sizeMultiplier(0.5f)// glide 加载图片大小 0~1之间 如设置 .glideOverride()无效
 * .setOutputCameraPath("/CustomPath")// 自定义拍照保存路径,可不填
 * .enableCrop()// 是否裁剪 true or false
 * .compress()// 是否压缩 true or false
 * .glideOverride()// int glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
 * .withAspectRatio()// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
 * .hideBottomControls()// 是否显示uCrop工具栏，默认不显示 true or false
 * .isGif()// 是否显示gif图片 true or false
 * .compressSavePath(getPath())//压缩图片保存地址
 * .freeStyleCropEnabled()// 裁剪框是否可拖拽 true or false
 * .circleDimmedLayer()// 是否圆形裁剪 true or false
 * .showCropFrame()// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
 * .showCropGrid()// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
 * .openClickSound()// 是否开启点击声音 true or false
 * .selectionMedia()// 是否传入已选图片 List<LocalMedia> list
 * .previewEggs()// 预览图片时 是否增强左右滑动图片体验(图片滑动一半即可看到上一张是否选中) true or false
 * .cropCompressQuality()// 裁剪压缩质量 默认90 int
 * .minimumCompressSize(100)// 小于100kb的图片不压缩
 * .synOrAsy(true)//同步true或异步false 压缩 默认同步
 * .cropWH()// 裁剪宽高比，设置如果大于图片本身宽高则无效 int
 * .rotateEnabled() // 裁剪是否可旋转图片 true or false
 * .scaleEnabled()// 裁剪是否可放大缩小图片 true or false
 * .videoQuality()// 视频录制质量 0 or 1 int
 * .videoMaxSecond(15)// 显示多少秒以内的视频or音频也可适用 int
 * .videoMinSecond(10)// 显示多少秒以内的视频or音频也可适用 int
 * .recordVideoSecond()//视频秒数录制 默认60s int
 * .isDragFrame(false)// 是否可拖动裁剪框(固定)
 * .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
 */
